<template>
	<view class="tab">
		<navigator open-type="switchTab" url="/pages/index/index" class="tab-box">
			首页
		</navigator>
		<navigator open-type="switchTab" url="/pages/follow/follow" class="tab-box">
			关注
		</navigator>
		<navigator url="/pages/upload/upload" class="tab-box">
			<view class="iconfont icon-jiahao font-box">
			</view>
		</navigator>
		<navigator open-type="switchTab" url="/pages/news/news" class="tab-box message">
			消息
			<view class="new" v-if='new_total != 0'>{{new_total | number}}</view>
		</navigator>
		<navigator open-type="switchTab" url="/pages/personal/personal" class="tab-box">
			我
		</navigator>
	</view>
</template>

<script>
	import t from '../common/helper.js';
	import IMService from "../lib/imservice.js";
	import {
		mapState,
		mapMutations
	} from 'vuex';

	export default {
		name: "tab",
		data() {
			return {
				video: '',
				conversations: '',
			};
		},
		created() {
			this.message();
		},
		filters: {
			number(data) {
				if (data > 99) {
					data = 99;
				}
				return data
			}
		},
		computed: {
			...mapState(['new_total']) //新消息
		},
		methods: {
			...mapMutations(['saveNew']),
			//监听消息
			message() {
				let currentUser = uni.getStorageSync('user')[0];
				if (this.goEasy.getConnectionStatus() == 'disconnected') {
					getApp().globalData.imService = new IMService(this.goEasy, this.GoEasy);
					getApp().globalData.imService.connect(currentUser);
				}
				//监听会话列表变化
				let self = this;
				this.goEasy.im.on(this.GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, (content) => {
					self.renderConversations(content);
				});
			},
			renderConversations(content) {
				this.conversations = content.conversations || [];
				let news = 0;
				this.conversations.forEach(item => {
					if (item.unread != 0) {
						news += item.unread;
					}
				})
				this.saveNew(news);
			}
		}
	}
</script>

<style lang="scss">
	.tab {
		width: 100%;
		color: #FFFFFF;
		background-color: #000000;
		display: flex;
		position: fixed;
		z-index: 90;
		left: 0;
		bottom: 0;
		height: 100px;
		line-height: 50px;
		text-align: center;

		.tab-box {
			flex: 1;

			.font-box {
				width: 70%;
				margin: 10px 15%;
				border-radius: 5px;
				height: 30px;
				line-height: 30px;
				background-color: #fff;
				color: #000000;
			}
		}

		.message {
			position: relative;

			.new {
				display: flex;
				align-items: center;
				justify-content: center;
				min-width: 16px;
				height: 16px;
				position: absolute;
				right: 5px;
				top: 3px;
				background-color: #fe2c55;
				border-radius: 50%;
				font-size: 10px;
			}
		}
	}
</style>
